package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (title,imgHref,content,cuisine_id) " +
                "values('" + productModel.getTitle() + "'," +
                "'" + productModel.getImgHref() + "'," +
                "'" + productModel.getContent() + "'," +
                "'" + productModel.getCuisineId() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public List<ProductModel> findAll(PageModel pageModel) {
        String sql = "select a.*,b.name as cuisine_name from product a,tab_cuisine b " +
                "where a.cuisine_id=b.id   " +
                "limit " + (pageModel.getPageNum() - 1) * pageModel.getPageSize() + "," + pageModel.getPageSize() + "";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setImgHref(resultSet.getString("imgHref"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineName(resultSet.getString("cuisine_name"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public ProductModel findById(Integer id) {


        String sql = "select a.*,b.name as cuisine_name from product a,tab_cuisine b where a.cuisine_id=b.id and a.id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        ProductModel model = new ProductModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setImgHref(resultSet.getString("imgHref"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                model.setCuisineName(resultSet.getString("cuisine_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int del(int id) {
        String sql = "delete from product where id=" + id;
        return JDBC.excuteUpdate(sql);
    }

    public int update(ProductModel productModel) {
        String sql = "update product set";
        if (productModel.getTitle() != null) {
            sql += " title='" + productModel.getTitle() + "',";
        }
        if (productModel.getImgHref() != null) {
            sql += " imgHref='" + productModel.getImgHref() + "',";
        }
        sql += " remark='' where id=" + productModel.getId();

        return JDBC.excuteUpdate(sql);
    }
    @Override
    public int getCount() {
        String sql = "select count(*) count from  product a,tab_cuisine b where a.cuisine_id=b.id";
        ResultSet resultSet = JDBC.getResultSet(sql);
        int count = 0;
        try {

            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
